package com.ft.oa.service.industryService.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ft.oa.domain.dto.sys.SecurityUser;
import com.ft.oa.domain.industrys.Industry;
import com.ft.oa.domain.industrys.IndustryContributor;
import com.ft.oa.domain.industrys.IndustryCustomerRelation;
import com.ft.oa.domain.sys.SysUser;
import com.ft.oa.enums.ManagerEnum;
import com.ft.oa.mapper.industrysMapper.IndustryContributorMapper;
import com.ft.oa.mapper.industrysMapper.IndustryCustomerRelationMapper;
import com.ft.oa.mapper.industrysMapper.IndustryMapper;
import com.ft.oa.mapper.sys.SysUserMapper;
import com.ft.oa.security.constant.SecurityConstant;
import com.ft.oa.service.industryService.IIndustryDocService;
import com.ft.oa.service.industryService.IIndustryService;
import com.ft.oa.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 售前行业维护Service业务层处理
 * 
 * @author baowz
 * @date 2021-12-30
 */
@Service
public class IndustryServiceImpl implements IIndustryService {

    @Resource
    private IndustryMapper industryMapper;
    @Resource
    private IndustryContributorMapper industryContributorMapper;
    @Resource
    private IndustryCustomerRelationMapper industryCustomerRelationMapper;
    @Autowired
    private IIndustryDocService iIndustryDocService;
    @Resource
    private SysUserMapper sysUserMapper;
    /**
     * 获取行业 维护的list 模糊查询行业主表
     */
    @Override
    public Map<String, Object> selectIndustryById(String reHyname, int page, int size) {
        //行业授权维护
        QueryWrapper<Industry> wrapper = new QueryWrapper();
        wrapper.eq("delldust","0");
        // 获取当前用户信息
        SecurityUser currentUser = SecurityUtils.getCurrentUser();
        boolean roledesc= false;
        // 获取当前用户名称用作展示售前项目管理
        Long creator = currentUser.getId();
        //获取用户权限 如果是admin 则是管理员 可以查看到全部
        Set<String> roles = currentUser.getRoles();
        for (String role:roles) {
            if(role.equals(SecurityConstant.SUPER_ROLE)){
                roledesc=true;
            }
        }
        // 判断行业名称是否为空
        boolean empty = StringUtils.isEmpty(reHyname);
        if(!empty){
            wrapper.like("hyname",reHyname);
        }
        if(roledesc){

        }else{
            if(ManagerEnum.GENERAL.toString().equals(SecurityUtils.getCurrentUser().getManager())){
                wrapper.eq("creator",SecurityUtils.getCurrentUser().getId());
            }
        }
        Page<Industry> pageIndustrytion = new Page<>(page, size);
        Page<Industry> IndustryPage = industryMapper.selectPage(pageIndustrytion, wrapper.orderByDesc("create_time"));
        //获取总数
        long total = IndustryPage.getTotal();
        HashMap<String,Object> totalMap = new HashMap<>();
        List<Industry> industries = IndustryPage.getRecords();
        List indlist = new ArrayList();
        industries.forEach(indus ->{
            HashMap<String,Object> indusmap = new HashMap<>();
            String UserId = indus.getResponsibilityUser();
            int responsibilityUserId = Integer.parseInt(UserId);
            //负责人id
            indusmap.put("responsibilityUserId",responsibilityUserId);
            String updateTime = indus.getUpdateTime();
            indusmap.put("newtime",updateTime);
            String responsibilityUser = indus.getResponsibilityUser();
            SysUser sysUser = sysUserMapper.selectById(responsibilityUser);
            Long id = indus.getId();
            indusmap.put("id",id);
            String principalname = sysUser.getNickname();
            //负责人
            indusmap.put("responsibilityUser",principalname);
            //行业名称
            String hyname = indus.getHyname();
            indusmap.put("hyname",hyname);
            //行业说明
            String hydesc = indus.getHydesc();
            indusmap.put("hydesc",hydesc);
            // 关联参与人员表
            Long industryid = indus.getId();
            //获取附件个数 行业背景
            indusmap.put("background",iIndustryDocService.docvaluenum(industryid, 1));
            //解决方案
            indusmap.put("solution",iIndustryDocService.docvaluenum(industryid, 2));
            //营销 策略
            indusmap.put("planning",iIndustryDocService.docvaluenum(industryid, 3));
            //经典案例
            indusmap.put("classic",iIndustryDocService.docvaluenum(industryid, 4));
            //行业授权维护参与人
            QueryWrapper<IndustryContributor> wrappercontri = new QueryWrapper();
            wrappercontri.eq("industry_id",industryid);
            wrappercontri.eq("dellpeople","0");
            Integer integer = industryContributorMapper.selectCount(wrappercontri);
            indusmap.put("cyrnum",integer);
            //行业授权维护目标客户表
            QueryWrapper<IndustryCustomerRelation> CustomerRelation = new QueryWrapper();
            CustomerRelation.eq("industry_id",industryid);
            CustomerRelation.eq("dellpeople","0");
            Integer khnum = industryCustomerRelationMapper.selectCount(CustomerRelation);
            indusmap.put("khmbnum",khnum);
            indlist.add(indusmap);
        });
        totalMap.put("total", total);
        totalMap.put("industryList",indlist);
        return totalMap;
    }

    /**
     * 添加行业主表信息
     * @author baowz
     */
    @Override
    public int addhyadvocate(Industry industry){
        // 获取当前用户名称用作展示售前项目管理
        Long id =  SecurityUtils.getCurrentUser().getId();
        industry.setCreator(id);
        return industryMapper.insert(industry);
    }

    /**
     * 删除行业维护主表信息
     * @param industId
     * @return
     */
    @Override
    public int dellhyadvocate(int industId) {
        UpdateWrapper<Industry> wrapper = new UpdateWrapper();
        wrapper.eq("id",industId);
        wrapper.set("delldust","1");
        return industryMapper.update(null,wrapper);
    }

    /**
     * 更新主表信息
     * @param industry
     */
    @Override
    public int updatehost(Industry industry){
        QueryWrapper<Industry> wrapper = new QueryWrapper();
        wrapper.eq("id",industry.getId());
        return industryMapper.update(industry, wrapper);
    }

    /**
     * 查询所有行业
     * @return
     */
    @Override
    public List<Industry> viewhyall() {
        QueryWrapper<Industry> wrapper = new QueryWrapper();
        wrapper.eq("delldust","0");
       return industryMapper.selectList(wrapper);
    }

    @Override
    public int indusnum() {
        // 获取当前用户名称用作展示售前项目管理
        Long creator = SecurityUtils.getCurrentUser().getId();
        //获取用户权限 如果是admin 则是管理员 可以查看到全部
        QueryWrapper<Industry> wrapper = new QueryWrapper<>();
        Set<String> roles = SecurityUtils.getCurrentUser().getRoles();
        for (String role:roles) {
            if(role.equals(SecurityConstant.SUPER_ROLE)){
                wrapper.eq("delldust","0");
            }else{
                wrapper.eq("delldust","0");
                wrapper.eq("responsibility_user",creator);
            }
        }
        return industryMapper.selectCount(wrapper);
    }

    @Override
    public Industry viewIndustry(String id) {
        return industryMapper.selectById(id);
    }
}
